%---------------------------------------------------------------
% random_element(+List, -Element)
% Given a list, this predicate returns a random element of said
% list. 
%---------------------------------------------------------------
random_element(List, Element) :-
  length(List, Length),
  rand_int(Length, Random),
  nth0(Random, List, Element).

%---------------------------------------------------------------
% nth0(+N, +List, -Elem)
% Is true when Elem is the Nth member of List, counting the 
% first as element 0.  (That is, throw away the first N elements 
% and unify Elem with the next.)  It can only be used to select 
% a particular element given the list and index.
%---------------------------------------------------------------
nth0(0, [Head|_], Head) :- !.

nth0(N, [_|Tail], Elem) :-
    nonvar(N),
    M is N-1,
    nth0(M, Tail, Elem).

nth0(N,[_|T],Item) :-       % Clause added KJ 4-5-87 to allow mode
    var(N),         % nth0(-,+,+)
    nth0(M,T,Item),
    N is M + 1.